package test;

import javax.microedition.lcdui.Image;
import org.xmap.ui.View;
import org.xmap.ui.ViewManager;
import org.xmap.ui.base.Window;
import org.xmap.ui.control.ImageGrid;
import org.xmap.ui.listener.ItemListener;

public class ImageGridView extends View implements ItemListener {

    private ImageGrid ig;

    public ImageGridView(View back) {
        super(back);
//        this.setBgColor(0x00ffff88);
        this.setTitleText("图片块控件演示");
        this.ig = new ImageGrid(0, 0, 0, 0, 29, Window.ORIENT_LEFT_UP);
        this.ig.setScretchMode(Window.SCRETCH_ROW | Window.SCRETCH_COLUMN);
        this.ig.setFocus(true);
        this.ig.setItemListener(this);
        for (int i = 0; i < 100; i++) {
            this.ig.addContent(i, null, this.createImage(i % 15));
        }
        this.addControls(ig);
        this.addLeftMenu("随机图片大小");
        this.addLeftMenu("背景模式", new String[]{"背景透明", "背景可见"});
        this.setRightMenu("返回");
    }

    public void keySelected(int id, Object obj) {
        this.setTitleText("按键选中：" + id);
    }

    public void pointerSelected(int id, Object obj) {
        this.setTitleText("触笔选中：" + id);
    }

    public void pointerDoubleSelected(int id, Object obj) {
    }

    public void commandEvent(String command, Window sender) {
        if (command.equals("边框显示")) {
            this.ig.setDrawRect(true);
        } else if (command.equals("边框隐藏")) {
            this.ig.setDrawRect(false);
        } else if (command.equals("随机图片大小")) {
            this.ig.setCellWidth((Math.abs((int) System.currentTimeMillis()) % 20) + 20);
        } else if (command.equals("背景透明")) {
            this.ig.setTransparent(true);
        } else if (command.equals("背景可见")) {
            this.ig.setTransparent(false);
        } else if (command.equals("返回")) {
            ViewManager.instance.setView(this.back, View.ANIMATION_LEFT_TO_RIGHT);
        }
    }

    private Image createImage(int value) {
        try {
            return Image.createImage("".getClass().getResourceAsStream("/xmapres/imagegrid/" + (value + 1) + ".png"));
        } catch (Exception e) {
            return null;
        }
    }
}
